package com.service.impl;

import com.dao.RoleDao;
import com.dao.impl.RoleDaoImpl;
import com.pojo.Role;
import com.pojo.User;
import com.service.RoleService;
import com.util.LayuiTable;
import com.vo.ResourceId;

import java.util.List;

public class RoleServiceImpl implements RoleService {
    RoleDao roleDao = new RoleDaoImpl();
    @Override
    public LayuiTable<Role> findAll(int page, int limit) {
        LayuiTable<Role> userLayuiTable = new LayuiTable<Role>();
        int cruentPage=(page-1)*limit;
        List<Role> userList = roleDao.getRoleAll(cruentPage,limit);
        userLayuiTable.setMsg("");
        userLayuiTable.setCode(0);
        userLayuiTable.setCount(roleDao.getRoleAll().size());
        userLayuiTable.setData(userList);
        return userLayuiTable;
    }

    @Override
    public List<ResourceId> findAllResourceId(int roleid) {
        return roleDao.getResourceIdAll(roleid);
    }

    @Override
    public int updateMenusByRoleId(int roleid,int[] mids) {
        int isflag = roleDao.deleteMenuByRoleId(roleid);
        try {
            if(isflag>0){
                for (int i = 0; i < mids.length; i++) {
                    roleDao.insertMenuByRoleId(roleid,mids[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
        return 1;
    }
}
